<?php  

session_start();
if(!isset($_SESSION['login'])){
   header("Location:index.php");
}
require_once('connect.php');

require_once('includes/notes.class.php');
require_once('includes/user.class.php');
$database = Database::getInstance();

$notes = new Notes();
$user = new User();
require_once('partials/header.php'); //wczytaj header i menu
?>


    <div class="container">

        <div class="row">

            <div class="col-md-3">
   
                <?php require_once('partials/content-left-menu.php'); ?>

            </div>

            <div class="col-md-9">

                <?php
                $alert = $_SESSION['alert'];
                unset($_SESSION['alert']);
                $user->show_alert($alert);
                ?>

				<h1>Aktualności</h1>
				<!--TYLKO DLA ADMINOW -->
				<?php
                if($user->is_leader()){
                    if(isset($_POST['addNote'])){
                        if(strlen($_POST['title'])>0 && strlen($_POST['text'])>0){
                            $title = $_POST['title'];
                            $text = $_POST['text'];
                            $student_id = $user->get_id();
                            $add = $notes->add_note($student_id,$title,$text);
                            if($add > 0){
                                header( 'Location:main.php');
                            }else{
                                $alert = array('Nie dodano posta...', 'danger');
                            }
                        }else{
                            $alert = array('Nie wypełniono tytułu lub treści wiadomości!', 'danger');
                        }

                        $user->show_alert($alert);
                    }
                    ?>
                    <button class="btn btn-info" id="add-news-toggle">Dodaj wiadomość</button>
                    <form name="AddNews" id="addNews"  method="post"class="news">
                        <div class="news-content">
                            <form action = "main.php" method = "post">
                                <input type="text" name = "title" placeholder="Tytuł newsa" class="news-title"/>
                                <textarea name ="text" placeholder="Treść newsa" class="news-text"></textarea>
                                <input type="hidden" name="addNote" value="true">
                                <input type="submit" value="Dodaj newsa" class="news-button"/>
                            </form>
                        </div>
                    </form>
                <?php
                }
                ?>
				<!--TYLKO DLA ADMINOW -->
				<?php
                    $uid = $user->get_user();
                    $lid = $user->find_leader($uid[0]['department'], $uid[0]['direction'], $uid[0]['year']);
					$notesData = $notes -> get_notes($lid);
					foreach($notesData as $note){
						$student= $notes->get_note_autor($note['student_id']);
						print '<div class="news">
								<div class="news-content">
									<span class="news-title" id="news-title-done-1">'.$note['title'].'</span><br>';
						print'<span class="news-author">Autor: <strong>'.$student.'</strong> | '.$note['added'].'</span>';
						print'<p class="news-text-done news-text">'.$note['text'].'</p>';
						print '</div>
									</div>	
										<div class="separator"></div>';
					}
                ?>
				
                </div>


                </div>

            </div>

        </div>

    </div>
 
<?php
require_once('partials/footer.php');
?>
